/**
 * File:   PutGetDataTab.java
 * Author: Gvennet
 */
package gui.tabs;

import dht.network.Network;
import dht.node.DHTDataModel;
import dht.node.DHTNodeModel;
import gui.actions.GetAction;
import gui.actions.PutAction;
import gui.main.DHTDataTableModel;
import gui.main.DHTNodeTableModel;

import javax.swing.*;
import java.awt.*;

/**
 * todo write javadoc
 */
public final class PutGetDataTab
{

	public static JComponent createPutGetDataTab(final DHTDataTableModel getDataTableModel, DHTNodeModel nodeModel, DHTDataModel dataModel, Network network)
	{
		JComponent component = new JComponent()
		{

		};

		component.setLayout(new BorderLayout());

		JTable dataTable = new JTable(getDataTableModel);
		dataTable.getColumnModel().getColumn(2).setCellRenderer(new DHTNodeTableModel.DateRenderer());
		JScrollPane scrollTable = new JScrollPane(dataTable);

		JTextField putField = new JTextField();
		PutAction putAction = new PutAction(nodeModel, putField, network);
		putField.setAction(putAction);
		JButton putButton = new JButton(putAction);

		JTextField getField = new JTextField();
		GetAction getAction = new GetAction(nodeModel, dataModel, getField, network, getDataTableModel);
		getField.setAction(getAction);
		JButton getButton = new JButton(getAction);

		GroupLayout layout = new GroupLayout(component);
		component.setLayout(layout);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();

		hGroup.addGroup(layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup().addComponent(putField).addComponent(putButton))
				.addGroup(layout.createSequentialGroup().addComponent(getField).addComponent(getButton))
				.addComponent(scrollTable));
		layout.setHorizontalGroup(hGroup);

		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

		vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).
				addComponent(putField).addComponent(putButton));
		vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).
				addComponent(getField).addComponent(getButton));
		vGroup.addComponent(scrollTable);
		layout.setVerticalGroup(vGroup);
		layout.linkSize(putButton, getButton);

		component.setVisible(true);

		return component;

	}

}
